package com.fjwt.gz.zhyx;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * 接口加密工具栏
 *
 * @author wuyb
 * @date
 */
@Slf4j
public class SignUtils {


    public static final String SIGN_TYPE = "0";

    public static final String API_KEY = "hnyzjr";

//    public static final String ZHYX_HOST_LOCAL = "http://localhost:20010";
//    public static final String ZHYX_HOST_SC = "http://10.177.9.50:10001/sfqq";

    private static final String ZHYX_KEY = "hnyzjrkey";




    /**
     * 参数加密
     * @param encStr  待加密参数
     * @param timestamp  时间戳
     * @return
     */
    public static String getEncryptSign(String encStr,long timestamp){

        return DigestUtils.md5DigestAsHex((encStr + ZHYX_KEY + timestamp).getBytes());
    }
    public static String getDecryptSign(String encStr,long timestamp){
        String s = AESUtils.getInstance().decrypt(encStr);
        return s;
    }


    public static String checkSign(Map<String, String> headerMap, String content){
        log.info("--加密入参:{}",content);
        //生成签名
        String apiKey = headerMap.get("auth-apikey");
        String secretKeyByApiKey = getSecretKeyByApiKey(apiKey);
        String time = headerMap.get("auth-timestamp");
        String sign = DigestUtils.md5DigestAsHex((content + secretKeyByApiKey + time).getBytes());
        String oldSign = headerMap.get("auth-sign");
        log.info("--获取加密结果:{};验签入参:{}-{}-{}",sign,oldSign,apiKey,time);
        //验签
        if (!sign.equals(oldSign)){
            return "签名错误！";
        }
        return "";
    }


    private static String getSecretKeyByApiKey(String key) {
        if (StringUtils.isNotEmpty(key) && API_KEY.equals(key)){
            return ZHYX_KEY;
        }
        return null;
    }


    /**
     * 获取参数
     * @param request
     * @return
     */
    public static String getContentByRequest(HttpServletRequest request) {
        try {
            BufferedReader reader = request.getReader();
            char[] buf = new char[512];
            int len = 0;
            StringBuffer contentBuffer = new StringBuffer();
            while ((len = reader.read(buf)) != -1) {
                contentBuffer.append(buf, 0, len);
            }
            String content = contentBuffer.toString();
            log.info("--获取请求内容：{}", content);
            return content;
        } catch (IOException e) {
            e.printStackTrace();
            log.error("[获取request中用POST方式“Content-type”是“text/plain”发送的json数据]异常:{}", e.getCause());
        }
        return "";
    }

    /**
     * 获取请求头
     *
     * @param request
     * @return
     */
    public static Map<String, String> getHeaderByRequest(HttpServletRequest request) {
        Enumeration<String> headerNames = request.getHeaderNames();
        Map<String, String> headerMap = new HashMap<>(10);
        while (headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            headerMap.put(name, request.getHeader(name));
        }
        return headerMap;
    }





}
